<?php
require_once ROOT_PATH.'boot/req_inc.php';

abstract class Setting implements IConfig,IRequestTool{
	protected $data;
	public  $app_name;
	public $cat_prefix='system_';
	public $cat_default='config';

	private $db_call;
	function set_db($db_call){ $this->db_call= $db_call; }
	function readData($cat,$var=NULL){
		//$db=call_user_func($this->db_call);
		$db = $GLOBALS['context']->db;
		$sql="SELECT code,value FROM ". $db->table($this->cat_prefix . $cat) ." WHERE type!='group'";
		return $db->getAll($sql);
	}

	function get($key,$default=NULL){
		if($this->data===NULL || ! $key) return $default;
		$cat=$code=NULL;
		$this->get_name($key,$cat,$code);
		$result=$this->get_group_var($cat,$code);
		if($result && isset($result[$code]) && !empty($result[$code])) return $result[$code];
		else return $default;
	}
	function get_var($varkey){
		return null;
	}
	function get_name($key,&$catlog,&$code){
		$gvn=explode('.',$key);
		if(count($gvn)<=1){
			$code=$key;
			$catlog=$this->cat_default;
		}else{
			$code=$gvn[1];
			$catlog=$gvn[0];
		}
	}
	abstract  function get_group_var($cat,$var);
}
class EmptySetting extends Setting{
	function __construct(){	$this->data=array();	}
	static function register($prop){ return new EmptySetting; }
	function get_group_var($cat,$var){
		if(isset($this->data[$var]) ) return $this->data[$var];
		$data=$this->readData($cat,$var);
		$vardata=array();
		foreach($data as $row){
			$vardata[ $row['code'] ] = $row['value'];
		}
		return $vardata;
	}
	function clearCache($key){}
}
class FileSetting extends Setting{
	private $conf='cache/data';
	private $conf_path;

	function __construct(){
		if(isset($GLOBALS['context']->app_name)) $this->app_name= $GLOBALS['context']->app_name;
		$this->conf_path=ROOT_PATH."{$this->app_name}/{$this->conf}/";
		if(! file_exists($this->conf_path)) mkdir($this->conf_path,0777,true);
		if(file_exists($this->conf_path)){ $this->data=array();}
		else $this->data=NULL;
	}
	static function register($prop){
		//$GLOBALS['context']->log_debug('FileSetting create');
		return new FileSetting();
	}

	function get_group_var($cat,$var){
		if($this->data===NULL) return ;
		if(isset($this->data[$var]) ) return $this->data[$var];

		$conf_file=$this->conf_path."{$cat}.php";
		if(file_exists($conf_file)){
			$$cat=NULL;
			include $conf_file;
			$data=$$cat;
			if($data){
				 $this->data[$var]=& $data;
				 return $data;
			}
		}
		$lines="<?php \n if (!defined('ROOT_PATH')) die('401,未授权访问 [Unauthorized]');\n";

		$data=$this->readData($cat,$var);
		$vardata=array();
		foreach($data as $row){
				$vardata[ $row['code'] ] = $row['value'];
		}

		$lines .= '$'. $cat .'='.var_export($vardata,true).";\n";
		if($lines){
			file_put_contents($conf_file,$lines);
		}
		return $vardata;
	}
	function clearCache($key){
		if($this->data===NULL) return ;
		$cat=$code=NULL;
		$this->get_name($key,$cat,$code);
		$conf_file=$this->conf_path."{$cat}.php";
		if(file_exists($conf_file))	unlink($conf_file);
	}
}
